var measure = {
    leftUp: function (movement, clickPosition) {
        /******************** 量测 ****************************/
        if (clickPosition) {
            if ((clickPosition.x - movement.position.x > 3) || (clickPosition.x - movement.position.x < -3) ||
                (clickPosition.y - movement.position.y > 3) || (clickPosition.y - movement.position.y < -3)) {
                clickPosition = undefined;
                return;
            }
        }

        if (isCoordinate) {
            var posit = scene.pickPosition(movement.position);
            var cartographic = LSGlobe.Cartographic.fromCartesian(posit);
            var currentClickLon = LSGlobe.Math.toDegrees(cartographic.longitude);
            var currentClickLat = LSGlobe.Math.toDegrees(cartographic.latitude);
            var currentClickHei = cartographic.height;
            var oCoordinate = {};
            oCoordinate.x = currentClickLon;
            oCoordinate.y = currentClickLat;
            oCoordinate.z = currentClickHei;
            //插入座标
            measure.insertCoordinate(oCoordinate);
            isCoordinate = false;
            $("#MeasureCoordinate").removeClass("active");

        } else if (heightMeasure && heightMeasure.enable) {
            if (heightMeasure.eMeasureStatus == 2) {
                $("#MeasureTriangle").removeClass("active");
            }
            heightMeasure.addPoint3D(movement);
        } else if (distance && distance.enable) {
            distance.addPoint3D(movement);
            //
        } else if (areaMeasure && areaMeasure.enable) {
            areaMeasure.addPoint3D(movement);
            //面积测量 
        }
    },
    rightClick: function (movement) {
        if (distance && distance.enable) {
            distance.finish();
            $("#MeasureDistance").removeClass("active");
        }
        if (areaMeasure && areaMeasure.enable) {
            areaMeasure.finish(movement);
            $("#MeasureArea").removeClass("active");
        }
    },
    mouseMove: function (movement) {
        if (heightMeasure && heightMeasure.enable) {
            heightMeasure.addPointTemp(movement);
        } else if (distance && distance.enable) {
            distance.addPointTemp(movement);

        } else if (areaMeasure && areaMeasure.enable) {
            areaMeasure.addPointTemp(movement);
        }
    },
    openContainer: function (obj) {
        var oThis = obj;
        if (oThis.hasClass("active")) {
            oThis.removeClass("active");
            $("#measure-container").hide();
        } else {
            //关闭所有设置项容器
            page.initialStatus("measure");
            $("#measure-container").show();
            oThis.parents("#switch-box").find(".bar-item").removeClass("active");
            oThis.addClass("active");
        }
    },
    closeContainer: function () {
        $("#measure-container").hide();
        $("#btnShowMeasure").removeClass("active");
    },
    clear: function () {
        for (var i = 0; i < distanceArray.length; i++) {
            distanceArray[i].clear();
        }
        for (var i = 0; i < heightMeasureArray.length; i++) {
            heightMeasureArray[i].clear();
        }
        for (var i = 0; i < areaMeasureArray.length; i++) {
            areaMeasureArray[i].clear();
        }
        $("#measure-container .measureItem").removeClass("active");
        isCoordinate = false;
        tempDataSource.entities.removeAll();
    },
    insertCoordinate: function (oPos) {
        var fHeight = oPos.z.toFixed(6);
        if (fHeight.length > 10) {
            fHeight = fHeight.substring(0, 10);
        } else {
            fHeight = fHeight.slice(-10)
        }
        var oLabelEntity = {
            name: "坐标测量",
            position: LSGlobe.Cartesian3.fromDegrees(oPos.x, oPos.y, oPos.z),
            clampToGround: true,
            attachPolygon: true,
            label: {
                text: "经度：" + ("   " + oPos.x.toFixed(6)).slice(-10) + "\u00B0\n纬度：" + ("   " + oPos.y.toFixed(6)).slice(-10) + "\u00B0\n高度：" + fHeight + "m ",
                translucencyByDistance: new LSGlobe.NearFarScalar(1.5e2, 2.0, 1.5e5, 0),
                style: LSGlobe.LabelStyle.FILL_AND_OUTLINE,
                font: '35px 楷体',
                fillColor: LSGlobe.Color.WHITE,
                outlineColor: LSGlobe.Color.BLACK,
                outlineWidth: 6,
                disableDepthTestDistance: 1000000000,
                scale: 0.5,
                pixelOffset: new LSGlobe.Cartesian2(0, -10),
                showBackground: true,
                backgroundColor: new LSGlobe.Color.fromCssColorString("rgba(0, 0, 0, 0.7)"),
                backgroundPadding: new LSGlobe.Cartesian2(10, 10),
                verticalOrigin: LSGlobe.VerticalOrigin.BOTTOM
            },
            point: {
                color: LSGlobe.Color.fromCssColorString("#FF0000"),
                pixelSize: 10,
                perPositionHeight: false,
                disableDepthTestDistance: 1000000000
            }
        };
        tempDataSource.entities.add(oLabelEntity);
    },
    init: function () {
        //测量 
        $("#btnShowMeasure").on("click", function () {
            disabledDantihua(false);
            measure.openContainer($(this));
        });
        //关闭测量
        $("#measure-container .comment-close").on("click", function () {
            measure.closeContainer($(this));
        });

        //清空测量
        $("#btnCleanMeasure").on("click", function () {
            measure.clear();
        });

        /*点击量测标签改变 */
        $("#measure-container .measureItem").on("click", function () {
            var sThis = $(this);
            //清理
            /* heightMeasure.clear();
            distance.clear();
            areaMeasure.clear();
            sThis.siblings().removeClass("active"); */
            if (distance) {
                distance = undefined;
            }
            if (heightMeasure) {
                heightMeasure = undefined;
            }
            if (heightMeasure) {
                heightMeasure = undefined;
            }

            $("#measure-container .measureItem").removeClass("active");

            if (sThis.hasClass("active")) {
                //测量开
                /* distance.enable = false;
                heightMeasure.enable = false;
                areaMeasure.enable = false; */

            } else {
                var sId = sThis.attr("id");
                sThis.addClass("active");
                if (sId == "MeasureDistance") {
                    distance = new LSGlobe.LSDistanceMeasure(viewer);
                    distance.enable = true;
                    distanceArray.push(distance);
                    isCoordinate = false;
                } else if (sId == "MeasureArea") {
                    areaMeasure = new LSGlobe.LSAreaMeasure(viewer);
                    areaMeasure.enable = true;
                    areaMeasureArray.push(areaMeasure);
                    isCoordinate = false;
                } else if (sId == "MeasureTriangle") {
                    heightMeasure = new LSGlobe.LSHeightMeasure(viewer);
                    heightMeasure.enable = true;
                    heightMeasureArray.push(heightMeasure);
                    isCoordinate = false;
                } else if (sId == "MeasureCoordinate") {
                    heightMeasure ? heightMeasure.enable = false : null;
                    areaMeasure ? areaMeasure.enable = false : null;
                    distance ? distance.enable = false : null;

                    isCoordinate = true;
                }
            }
        });
    }
}